﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using Common;
using Controllers.Extend;
using Controllers.Filter;
using Model.Database;
using Model.Sys.Config;
using Newtonsoft.Json;
using SearchModel;
using ViewModel;

namespace Controllers.Areas.TreeData.Controllers
{
    public class ManagerController : UserBaseController
    {
        protected override void OnAuthorization(AuthorizationContext filterContext)
        {
            cookieName = CookieConfig.UserInfoLoginCookieName;
            base.OnAuthorization(filterContext);

            if (loginUser == null)
            {
                if (filterContext.RequestContext.HttpContext.Request.IsAjaxRequest())
                {
                    filterContext.Result = new JsonResult()
                    {
                        ContentEncoding = Encoding.UTF8,
                        Data = new ResultModel
                        {
                            Pass = false,
                            Msg = "对不起，您的身份验证过时，请重新登录。"
                        },
                        JsonRequestBehavior = JsonRequestBehavior.AllowGet
                    };
                }
                else
                {
                    filterContext.Result = new ContentResult() { Content = "<script>alert('对不起，您的身份验证过时，请重新登录。');top.location='/Login/Index';</script>" };
                }
            }
        }
        
        /// <summary>
        /// 树状数据的列表页面
        /// </summary>
        /// <param name="powerId"></param>
        /// <returns></returns>
        [Power(PowerId = "01000800")]
        public ActionResult Index(string powerId)
        {
            ViewData["powerId"] = powerId;
            return View();
        }

        /// <summary>
        /// 列表页面的列表查询
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public ActionResult HeadSearch(DataTable dt)
        {
            int total;
            int pageSize = dt.length;
            int pageIndex = dt.pageIndex;
            SMTreeDataHead smTreeDataHead = JsonConvert.DeserializeObject<SMTreeDataHead>(dt.searchStr);

            List<TreeDataHead> dataDictionaryHeadList = CurrentBllSession.TreeDataHeadBll.GetListBySearchPage(pageIndex, pageSize, out total, smTreeDataHead);
            dt.recordsTotal = total;
            dt.recordsFiltered = total;
            dt.data = dataDictionaryHeadList;

            return Json(dt);
        }

        /// <summary>
        /// 增加树状数据头页面
        /// </summary>
        /// <returns></returns>
        [Power(PowerId = "01000801")]
        public ActionResult AddHead()
        {
            return View();
        }

        /// <summary>
        /// 增加树状数据头提交
        /// </summary>
        /// <param name="pmTreeDataHead"></param>
        /// <returns></returns>
        public ActionResult AddHeadSubmit(TreeDataHead pmTreeDataHead)
        {
            ResultModel rm = new ResultModel();

            if (!string.IsNullOrEmpty(pmTreeDataHead.Name))
            {
                TreeDataHead treeDataHead = pmTreeDataHead;

                if (CurrentBllSession.TreeDataHeadBll.GetIQueryable().Count(a => a.Name == treeDataHead.Name) > 0)
                {
                    rm.Pass = false;
                    rm.Msg = "树状数据头名称已存在";
                }
                else
                {
                    if (!treeDataHead.Sort.HasValue)
                    {
                        treeDataHead.Sort = MakeSortForHead();
                    }
                    treeDataHead.AddTime = DateTime.Now;
                    CurrentBllSession.TreeDataHeadBll.Add(treeDataHead);
                    //CurrentBllSession.TreeDataHeadBll.ResetTreeData();
                    rm.Pass = true;
                    rm.Msg = "添加成功";
                }
            }
            else
            {
                rm.Pass = false;
                rm.Msg = "请填写完整信息";
            }

            return Json(rm);
        }


















        #region 私有方法
        /// <summary>
        /// 制作树状数据头排序号
        /// </summary>
        /// <returns></returns>
        private int MakeSortForHead()
        {
            int sort = 1;

            int? maxSort = CurrentBllSession.TreeDataHeadBll.GetIQueryable().Max(a => a.Sort);
            if (maxSort.HasValue)
            {
                sort = maxSort.Value + 1;
            }

            return sort;
        }
        #endregion

    }
}
